﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using DeviceInfoManage.BusinessLogicLayer;
public partial class DeviceRepairOut : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        /*首次进入本页时对设备类别，设备信息下拉框和设备图片进行初始化*/
        if (!IsPostBack)
        {
            if (Session["adminUserName"] == null)
            {
                Response.Redirect("login.aspx");
            }
            /*查询所有设备类型信息然后绑定到设备类别下拉框中*/
            this.DeviceType.DataSource = (new DeviceType()).GetDeviceType();
            this.DeviceType.DataValueField = "deviceTypeId";
            this.DeviceType.DataTextField = "deviceTypeName";
            this.DeviceType.DataBind();

            /*取得第一个设备类型的编号*/
            string deviceTypeId = this.DeviceType.Items[0].Value;
            /*查询该设备类别下所有设备信息然后绑定到设备信息下拉框中*/
            DataSet ds = (new DeviceInfo()).QueryDevice(Int32.Parse(deviceTypeId));
            this.Device.DataSource = ds;
            this.Device.DataTextField = "deviceName";
            this.Device.DataValueField = "deviceId";
            this.Device.DataBind();
            /*如果该类别下没有设备信息就显示默认的图片*/
            if (this.Device.Items.Count == 0)
                this.DeviceImage.ImageUrl = "images/NoImage.jpg";
            else
            {
                /*如果该类别下有设备获取该类下第一个设备的设备编号*/
                string deviceId = this.Device.Items[0].Value;
                string deviceImagePath = (new DeviceInfo()).GetDeviceImagePath(Int32.Parse(deviceId)); /*得到该设备的图片路径*/
                /*如果该设备没有图片就显示默认的图片*/
                if (deviceImagePath == "")
                    this.DeviceImage.ImageUrl = "images/NoImage.jpg";
                /*如果该设备有图片就显示*/
                else
                    this.DeviceImage.ImageUrl = deviceImagePath;
            }

        }
    }

    /*选择不同的设备时如果该设备有图片则在Image控件中显示该设备的图片*/
    protected void Device_SelectedIndexChanged(object sender, EventArgs e)
    {
        /*获取选择设备的设备编号*/
        string deviceId = this.Device.SelectedValue;
        string deviceImagePath = (new DeviceInfo()).GetDeviceImagePath(Int32.Parse(deviceId)); /*得到该设备的图片路径*/
        /*如果该设备没有图片就显示默认的图片*/
        if (deviceImagePath == "")
            this.DeviceImage.ImageUrl = "images/NoImage.jpg";
        /*如果该设备有图片就显示*/
        else
            this.DeviceImage.ImageUrl = deviceImagePath;

    }
    protected void Btn_RepairOut_Click(object sender, EventArgs e)
    {
        /*建立一个维修信息对象,接受出修信息的各个字段信息*/
        RepairInfo repairInfo = new RepairInfo();
        if (this.Device.SelectedValue == "")
        {
            Response.Write("<script>alert('请选择出修的设备!');</script>");
            return;
        }
        repairInfo.DeviceId = Int32.Parse(this.Device.SelectedValue);
        repairInfo.RepairPlace = this.RepairPlace.Text;
        repairInfo.RepairMan = this.RepairMan.Text;
        repairInfo.ErrReason = this.ErrReason.Text;
        repairInfo.RepairStartTime = Convert.ToDateTime(this.RepairStartTime.Text);
        repairInfo.RepairMemo = this.RepairMemo.Text;
        /*然后调用业务层的AddRepairInfo函数加入*/
        if (repairInfo.AddRepairInfo())
            Response.Write("<script>alert('设备出修成功!');location.href='DeviceRepairOut.aspx';</script>");
        else
            Response.Write("<script>alert('" + repairInfo.ErrMessage + "');</script>");
    }
    protected void DeviceType_SelectedIndexChanged(object sender, EventArgs e)
    {
        /*取得选择的设备类型的编号*/
        string deviceTypeId = this.DeviceType.SelectedValue;
        /*查询该设备类别下所有设备信息然后绑定到设备信息下拉框中*/
        DataSet ds = (new DeviceInfo()).QueryDevice(Int32.Parse(deviceTypeId));
        this.Device.DataSource = ds;
        this.Device.DataTextField = "deviceName";
        this.Device.DataValueField = "deviceId";
        this.Device.DataBind();
        /*如果该类别下没有设备信息就显示默认的图片*/
        if (this.Device.Items.Count == 0)
            this.DeviceImage.ImageUrl = "images/NoImage.jpg";
        else
        {
            /*如果该类别下有设备获取该类下第一个设备的设备编号*/
            string deviceId = this.Device.Items[0].Value;
            string deviceImagePath = (new DeviceInfo()).GetDeviceImagePath(Int32.Parse(deviceId)); /*得到该设备的图片路径*/
            /*如果该设备没有图片就显示默认的图片*/
            if (deviceImagePath == "")
                this.DeviceImage.ImageUrl = "images/NoImage.jpg";
            /*如果该设备有图片就显示*/
            else
                this.DeviceImage.ImageUrl = deviceImagePath;
        }

    }
}
